package com.stox.repository.object;

import org.springframework.stereotype.Component;

import com.db4o.ObjectSet;
import com.stox.model.ExchangeDownloadMetadata;
import com.stox.repository.ExchangeDownloadMetadataRepository;

@Component
public class ExchangeDownloadMetadataObjectRepository extends
		AbstractObjectRepository implements ExchangeDownloadMetadataRepository{

	public ExchangeDownloadMetadata findByExchangeCode(String exchangeCode) {
		ObjectSet<ExchangeDownloadMetadata> result = database.queryByExample(new ExchangeDownloadMetadata(exchangeCode, null));
		return result.hasNext() ? result.next() : null;
	}

	public ExchangeDownloadMetadata save(
			ExchangeDownloadMetadata exchangeDownloadMetadata) {
		ExchangeDownloadMetadata managed = findByExchangeCode(exchangeDownloadMetadata.getExchangeCode());
		if(null != managed){
			managed.setLastDownloadDate(exchangeDownloadMetadata.getLastDownloadDate());
		}else{
			managed = exchangeDownloadMetadata;
		}
		database.store(managed);
		database.commit();
		return managed;
	}

}
